package com.application.npc.manage;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;

import com.application.map.manage.MapManage;
import com.application.mission.manage.MissionManage;
import com.application.npc.pojo.Npc;
import com.application.world.manage.WorldManage;
import com.core.utils.Constants;

public class NpcManage {

	public static Map<Integer, Npc> npcs = new ConcurrentHashMap<Integer, Npc>();
	private static Logger logger = Logger.getLogger(MissionManage.class);
	private static final String DATA_FILE_PATH = "res/npc/npc.dat";

	public static void init() {
		FileInputStream fis = null;
		BufferedReader bf = null;
		try {
			System.out.println("init npc start ++++");
			fis = new FileInputStream(DATA_FILE_PATH);
			InputStreamReader isr = new InputStreamReader(fis,
					Constants.DEFAULT_CHARSET);
			bf = new BufferedReader(isr);
			String line = bf.readLine();
			if(line!=null)line=line.trim();
			while (line != null && line.compareTo("") != 0) {
				if (!line.startsWith("#")) {
					String[] params = line.split(",");
					Npc n = new Npc();
					n.npcId= Integer.parseInt(params[0].trim());
					n.mapId = Integer.parseInt(params[1].trim());
					n.x = Integer.parseInt(params[2].trim());
					n.y = Integer.parseInt(params[3].trim());
 
					
					npcs.put(n.npcId, n);
					MapManage map = WorldManage.getMapManage(n.mapId);
					if(map!=null)
						map.npcMap.put(n.npcId,n);
					System.out.println("npc " + n.npcId);
				}
				line = bf.readLine();
				if(line!=null)line=line.trim();
			}
			System.out.println("init npc end ----");

		} catch (Exception e) {
			logger.error("",e);
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					logger.error("",e);
				}
			}
			if (bf != null) {
				try {
					bf.close();
				} catch (IOException e) {
					logger.error("",e);
				}
			}
		}
	}

}
